package com.hzye.framework.common.util;

import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.LinkedList;

/***
 * 获取服务器的mac地址
 * 
 * @author Administrator
 * 
 */
public class MacHelper {
	public static LinkedList<String> getMac() {
		LinkedList list = new LinkedList();
		try {
			Enumeration el = NetworkInterface.getNetworkInterfaces();

			while (el.hasMoreElements()) {
				byte[] mac = ((NetworkInterface) el.nextElement())
						.getHardwareAddress();

				if (mac == null) {
					continue;
				}
				boolean firstFlag = true;
				StringBuilder builder = new StringBuilder();

				for (byte b : mac) {
					if (firstFlag) {
						firstFlag = false;
						builder.append(hexByte(b));
					} else {
						builder.append("-").append(hexByte(b));
					}

				}

				if ((builder.toString().trim().length() <= 0)
						|| (!ParamsValidate.isMac2(builder.toString()
								.toUpperCase()))
						|| (builder.toString().toUpperCase()
								.equals("00-00-00-00-00-00-00-E0"))
						|| (list.contains(builder.toString().toUpperCase())))
					continue;
				list.add(builder.toString().toUpperCase());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
	
	 private static String hexByte(byte b)
	  {
	    String s = "000000" + Integer.toHexString(b);

	    return s.substring(s.length() - 2);
	  }
}
